package com.ss.android.article.browser.main;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.MotionEventCompat;
import android.support.v4.view.ViewCompat;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.animation.AnimationUtils;
import android.widget.OverScroller;

import com.bytedance.common.utility.Logger;

public final class MotionDirectionScrollHelper {
    public static abstract class MotionHeaderScroller {
        public MotionHeaderScroller() {
            super();
        }

        public int a() {
            return 0;
        }

        public void a(boolean arg1) {
        }

        public boolean a(int arg2) {
            return false;
        }

        public int b() {
            return 0;
        }

        public void b(int arg1) {
        }

        public boolean c(int arg2) {
            return false;
        }
    }

    public static final class TouchEventDispatcher {
        private final MotionDirectionFrameLayout mMotionDirectionFrameLayout;

        TouchEventDispatcher(MotionDirectionFrameLayout motionDirectionFrameLayout) {
            this.mMotionDirectionFrameLayout = motionDirectionFrameLayout;
        }

        public final boolean dispatchTouchEvent(MotionEvent motionEvent) {
            return mMotionDirectionFrameLayout.dispatchTouchEvent(this.mMotionDirectionFrameLayout, motionEvent);
        }
    }

    final OverScroller mOverScroller;
    final View mTargetView;
    VelocityTracker mVelocityTracker;
    MotionHeaderScroller d;
    int e;
    private final TouchEventDispatcher mTouchEventDispatcher;
    private int mScaledTouchSlop;
    private float h;
    private int i;
    private int j;
    private long k;
    private int l;

    public MotionDirectionScrollHelper(@NonNull Context context, @NonNull View view, TouchEventDispatcher touchEventDispatcher) {
        super();
        this.i = -2147483648;
        this.j = -1;
        this.k = -1;
        this.l = -2147483648;
        this.mTargetView = view;
        this.mOverScroller = new OverScroller(context, AnimationUtils.loadInterpolator(context, android.R.interpolator.decelerate_quint));

        this.mScaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        this.mTouchEventDispatcher = touchEventDispatcher;
        this.mTargetView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {

            public final void onViewAttachedToWindow(View v) {
            }

            public final void onViewDetachedFromWindow(View v) {
                if (MotionDirectionScrollHelper.this.mVelocityTracker != null) {
                    MotionDirectionScrollHelper.this.mVelocityTracker.recycle();
                    MotionDirectionScrollHelper.this.mVelocityTracker = null;
                }
                MotionDirectionScrollHelper.this.mOverScroller.abortAnimation();
            }
        });
    }

    public final boolean a(float arg8) {
        boolean v0;
        boolean v1 = false;
        if (this.d != null) {
            this.mOverScroller.abortAnimation();
            int v3 = this.d.a();
            int v2 = this.d.b();
            if (arg8 == 0f) {
                if (this.j != 1 && this.j != 2) {
                    v0 = v2 < v3 / 2 ? true : false;
                } else {
                    if (this.j == 1) {
                        v0 = true;
                    } else {
                        v0 = false;
                    }
                }
            } else {
                if (arg8 < 0f) {
                    v0 = true;
                } else {
                    v0 = false;
                }
            }
            int v4 = v0 ? v3 - v2 : -v2;
            this.d.a(v0);
            if (v4 == 0) {
                return v1;
            }
            v3 = arg8 != 0f ? Math.round(1000f * Math.abs((((float) v4)) / arg8)) * 4 : 0;
            this.mOverScroller.startScroll(0, v2, 0, v4, Math.min(Math.max(v3, 300), 600));
            ViewCompat.postInvalidateOnAnimation(this.mTargetView);
            v1 = true;
        }

        return v1;
    }

    public final boolean dispatchTouchEvent(@NonNull MotionEvent motionEvent) {
        int v3 = 2;
        int v0 = -1;
        int v8 = -2147483648;
        boolean v2 = true;
        if (this.d == null) {
            v2 = false;
        } else {
            int action = MotionEventCompat.getActionMasked(motionEvent);
            if (this.mVelocityTracker == null) {
                this.mVelocityTracker = VelocityTracker.obtain();
            }

            switch (action) {
                case 2: {
                    if (this.l != v8 && Math.abs(motionEvent.getY() - (((float) this.l))) > (((float) this.mScaledTouchSlop))) {
                        this.l = v8;
                    }

                    float v5 = motionEvent.getRawY();
                    boolean condition8 = this.b(motionEvent);

                    if (condition8) {
                        if (this.i != v8) {
                            float v6 = motionEvent.getY();
                            float v7 = motionEvent.getX();
                            motionEvent.setLocation(v7, v5);
                            this.mVelocityTracker.addMovement(motionEvent);
                            motionEvent.setLocation(v7, v6);
                            boolean condition = !this.d.a((((int) v5)) - this.i);
                            if (condition) {
                                if (this.j != v0) {
                                    v0 = 0;
                                }
                                this.j = v0;
                                this.k = -1;
                                if (this.mTouchEventDispatcher == null) {
                                } else {
                                    if (this.e == 0) {
                                    } else {
                                        if ((this.e & 1) != 1) {
                                            v0 = 0;
                                        } else if (this.d.a() == this.d.b()) {
                                            v0 = 1;
                                        } else {
                                            v0 = 0;
                                        }

                                        if (v0 == 0 && (this.e & 2) == v3) {
                                            v0 = this.d.b() == 0 ? 1 : 0;
                                        }

                                        if (v0 == 0) {
                                        } else {
                                            motionEvent.setAction(0);
                                            this.mTouchEventDispatcher.dispatchTouchEvent(motionEvent);
                                            motionEvent.setAction(action);
                                            this.l = ((int) v6);
                                        }
                                    }
                                }
                            }
                        }
                        this.i = ((int) v5);
                        return v2;
                    }

                    if (this.j == v0) {
                        float v0_1 = v5 - this.h;
                        if (Math.abs(v0_1) < (((float) this.mScaledTouchSlop))) {
                            v0 = this.j;
                        } else if (v0_1 > 0f) {
                            v0 = v3;
                        } else {
                            v0 = 1;
                        }
                    } else {
                        if (this.i != v8 && v5 != (((float) this.i))) {
                            if (v5 - (((float) this.i)) <= 0f) {
                                v3 = 1;
                            }
                            v0 = v3;
                        } else {
                            v0 = this.j;
                        }
                    }
                    this.i = ((int) v5);
                    if (v0 <= 0) {
                        return false;
                    }

                    if (v0 == this.j) {
                        return false;
                    }

                    this.j = v0;
                    boolean condition9 = !this.d.c(v0);
                    if (condition9) {
                        return false;
                    }

                    this.k = motionEvent.getDownTime();
                    if (this.mTouchEventDispatcher != null) {
                        motionEvent.setAction(3);
                        this.mTouchEventDispatcher.dispatchTouchEvent(motionEvent);
                        motionEvent.setAction(action);
                    }
                    this.mOverScroller.abortAnimation();
                    return v2;
                }
                case 0: {
                    this.h = motionEvent.getRawY();
                    this.mVelocityTracker.clear();
                }
                default: {
                    if (action != 0 && (this.b(motionEvent))) {
                        this.mVelocityTracker.computeCurrentVelocity(1000);
                        this.a(this.mVelocityTracker.getYVelocity());
                        this.mVelocityTracker.clear();
                    }
                    this.i = v8;
                    this.j = v0;
                    this.k = -1;
                    if (action == 1 && this.l != v8 && this.mTouchEventDispatcher != null) {
                        motionEvent.setAction(3);
                        this.mTouchEventDispatcher.dispatchTouchEvent(motionEvent);
                        motionEvent.setAction(action);
                        this.l = v8;
                        return v2;
                    }

                    this.l = v8;
                    break;
                }
            }
            v2 = false;
        }

        return v2;
    }

    private boolean b(MotionEvent arg5) {
        boolean v0 = this.k <= 0 || this.k != arg5.getDownTime() ? false : true;
        return v0;
    }
}


